<?php
class top_ctrl extends Controller
{

    /////////////////////////////商品接口/////////////////////////////////////////////


    /************************
     * 查询商品折扣信息
     ************************/
    private function get_items_ump($item)
    {

        $_mdl = Loader::top('items_top');

        $id = $item->num_iid;

        //取缓存
        $ccItemUmpPirceKey = $id . '_ump_min_price';
        ccDel($ccItemUmpPirceKey);
        $ccItemUmpPirceValue = ccLoad($ccItemUmpPirceKey);
        if (!$ccItemUmpPirceValue) {
            $resp = $_mdl->UmpPromotionGetRequest($id);
            $umps = array(); //该商品可能有多个促销活动
            $prices = array();

            if (isset($resp->promotions->promotion_in_item->promotion_in_item)) {
                $umps = $resp->promotions->promotion_in_item->promotion_in_item;
                if (count($umps) > 0) {
                    //得到全部折扣价,并过滤会员折扣
                    foreach ($umps as $ump) {
                        $pid = $ump->promotion_id;
                        if (!stristr($pid, 'shopvip')) {
                            array_push($prices, $ump->item_promo_price);
                        }
                    }
                }
            }
            //只取最低那个折扣价
            if (count($prices) > 0) $ccItemUmpPirceValue = min($prices);


        }
        //赋值给当前商品对象
        $item->ump_price = $ccItemUmpPirceValue ? $ccItemUmpPirceValue : $item->price;

        //存缓存,3小时
        ccSave($ccItemUmpPirceKey, $item->ump_price, 3600);

        //计算折扣
        //$item->rate = round(($item->ump_price / $item->price) * 10, 1);
        //sleep(1);


        return $item;
    }


    /************************
     * 图片分类
     ************************/
    public function image_cats()
    {
        if (empty($this->tb_nick)) {
            $this->ajax('', array(1, '未登录', 1, '请先登录应用.'));
        } else {

            $key = $this->tb_nick . '_picturecats';
            ccDel($key);
            $all_cats = ccLoad($key);
            //如果缓存中存在数据,直接从缓存中读取数据
            if ($all_cats != false) {
                //$all_cats->getmode = 'cache';
                //var_dump($result->items->item);
                return $this->ajax($all_cats);
            } else {
                $_mdl = Loader::top('pics_top');
                $rs_cats = $_mdl->PictureCategoryGetRequest();

                //分类收集
                $pics_cats = o2a($rs_cats->picture_categories->picture_category);


                $all_cats = array();
                foreach ($pics_cats as $cat) {
                    $tmp_sub_cats = array();

                    $cat_name = $cat['picture_category_name'];
                    $cat_name = mb_ereg_replace('^(　| )+', '', $cat_name);
                    $cat_name = mb_ereg_replace('(　| )+$', '', $cat_name);

                    /*if ($cat['parent_id'] == 0) {
                        if($all_cats[$cat['picture_category_id']]){
                            $all_cats[$cat['picture_category_id']]['name'] = $cat_name;
                        }else{
                            array_push($all_cats,
                                array(
                                    'id' => $cat['picture_category_id'],
                                    'name' => $cat_name,
                                    'sub_cats' => $tmp_sub_cats
                                )
                            );
                        }
                    }else{
                        if($all_cats[$cat['parent_id']]){
                            array_push($all_cats[$cat['parent_id']['sub_cats']], array(
                                'id' => $cat['picture_category_id'],
                                'name' => $cat_name,
                                'sub_cats' => $tmp_sub_cats
                            ));
                        }else{
                            foreach ($pics_cats as $cat2) {
                                if($cat2['picture_category_id'] == $cat['parent_id']){
                                    $cat2_name = $cat['picture_category_name'];
                                    $cat2_name = mb_ereg_replace('^(　| )+', '', $cat_name);
                                    $cat2_name = mb_ereg_replace('(　| )+$', '', $cat_name);
                                }
                            }

                            array_push($all_cats,
                                array(
                                    'id' => $cat['parent_id'],
                                    'name' => '',
                                    'sub_cats' => $tmp_sub_cats
                                )
                            );
                        }
                    }*/


                    $all_cats[$cat['picture_category_id']] = array(
                        'parent_id' => strval($cat['parent_id']),
                        'picture_category_id' => strval($cat['picture_category_id']),
                        'name' => $cat_name,
                        'sub_cats' => $tmp_sub_cats,
                        'children_id' => 0
                    );
                    if ($cat['parent_id'] != 0) {
                        $all_cats[$cat['parent_id']]['children_id'] = $cat['parent_id'];
                    }

                }


                function make($cats)
                {
                    $i = 0;
                    $edit = array();
                    $del = array();
                    foreach ($cats as $cat) {
                        if ($cat['children_id'] == 0 && $cat['parent_id'] != 0) {
                            $cats[$cat['parent_id']]['sub_cats'][$cat['picture_category_id']] = $cat;
                            array_push($edit, $cat['parent_id']);
                            array_push($del, $cat['picture_category_id']);
                        }
                        $i++;
                    }


                    $edit = array_unique($edit);

                    foreach ($del as $one) {
                        $ii = 0;
                        foreach ($cats as $cat) {
                            if ($cat['picture_category_id'] == $one) {
                                unset($cats[$cat['picture_category_id']]);
                            }
                            $ii++;
                        }
                    }

                    //dump($edit);
                    foreach ($edit as $one) {
                        foreach ($cats as $cat) {
                            if ($cat['picture_category_id'] == $one) {
                                //dump($cats[$cat['picture_category_id']]);
                                $cats[$cat['picture_category_id']]['children_id'] = 0;
                                $cats[$cat['picture_category_id']]['sub_cats'] = array_values($cats[$cat['picture_category_id']]['sub_cats']);
                            }
                        }
                    }


                    if (count($edit) < 1) {
                        return $cats;
                    } else {
                        return make($cats);
                        //return $cats;
                    }
                }

                //dump(make($all_cats));
                $all_cats = array_values(make($all_cats));
                ccSave($key, $all_cats, 600);
                //$all_cats->getmode = 'API';

                //$rs_pics->cats = $rs_cats->picture_categories->picture_category;
                $this->ajax($all_cats);


            }

        }
    }

    /************************
     * 列表图片
     ************************/
    public function image_list()
    {
        if (empty($this->tb_nick)) {
            $this->ajax('', array(1, '未登录', 1, '请先登录应用.'));
        } else {
            $title = "";
            $cid = 0;
            $pageno = 1;
            $pagesize = 16;

            if ($_REQUEST['keyWord']) $title = urldecode($_REQUEST['keyWord']);
            if ($_REQUEST['catIds']) $cid = $_REQUEST['catIds'];
            if ($_REQUEST['pageNo']) $pageno = (int)$_REQUEST['pageNo'];
            if ($_REQUEST['pageSize']) $pagesize = (int)$_REQUEST['pageSize'];

            $key = $this->tb_nick . '_pictures_' . $pageno . '_' . $pagesize . '_' . $title . '_' . $cid;
            ccDel($key);
            $rs_items = ccLoad($key);
            //如果缓存中存在数据,直接从缓存中读取数据
            if ($rs_items != false) {
                //$rs_items->getmode = 'cache';
                //var_dump($result->items->item);
                return $this->ajax($rs_items);
            } else {
                $all_pics = array();
                $_mdl = Loader::top('pics_top');

                $rs_items = $_mdl->PictureGetRequest($title, $pageno, $pagesize, $cid);
                if ($rs_items->pictures->picture) {
                    $pics = json_decode(json_encode($rs_items->pictures->picture), true);

                    foreach ($pics as $pic) {
                        $pic['picture_path'] = preg_replace("/(.*?:)(.*)/", "$2", $pic['picture_path']);
                        array_push($all_pics, $pic);
                    }
                }
                $rs_items->pictures->picture = $all_pics;

                ccSave($key, $rs_items, 3600);
                //$rs_items->getmode = 'API';
            }

            $this->ajax($rs_items);
        }
    }


    public function get_traderate(){
        if (empty($this->tb_nick)) {
            return $this->ajax('', array(1, '未登录', 1, '请先登录应用.'));
        } else {
            if (isset($_REQUEST["itemId"])) {
                $pageSize = isset($_REQUEST["pageSize"]) ? $_REQUEST["pageSize"] : 10;
                $pageNo = isset($_REQUEST["pageNo"]) ? $_REQUEST["pageNo"] : 1;
                $itemId = $_REQUEST["itemId"];

                $data = $this->get_top_traderate($itemId, $pageNo, $pageSize);

                return $this->ajax($data);
            }
        }
    }


    public function get_top_traderate($itemId, $pageNo, $pageSize){
        if (empty($this->tb_nick)) {
            return $this->ajax('', array(1, '未登录', 1, '请先登录应用.'));
        } else {
            $_mdl = Loader::top('traderate_top');

            $traderate = $_mdl->BuyshowShowGetbyitemRequest($itemId, $pageNo, $pageSize);
            $traderate = json_decode('{
                            "result": {
                                "err_trace": "",
                                "bsuccess": true,
                                "ret_code": {
                                    "code": 0,
                                    "msg": "success"
                                },
                                "module": {
                                    "total_record": 1,
                                    "total_page": 1,
                                    "list": {
                                        "buyshow": [
                                            {
                                                "picture": "http://img01.daily.taobaocdn.net/tfscom/i2/15782035348358/TB2BadXXXXXXXXEXXXXXXXXXXXX_ !!2066955782-0-rate.jpg",
                                                "pic_meta": "[{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i4/1862941194/TB2ER9mpVXXXXaCXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629},{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i2/1862941194/TB2PVygpVXXXXbBXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629},{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i2/1862941194/TB2t.9bpVXXXXbEXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629},{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i1/1862941194/TB2GyXQpVXXXXbvXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629}]",
                                                "item_id": 1500017235792,
                                                "id": 123,
                                                "description": "第一条发货快发货快发货快发货快发货快发货快发货快发货快发货快发货快",
                                                "seller_id": 2065340343,
                                                "buyer_nick": "1r***1",
                                                "mix_buyer_nick": "r01HKzPtEFlPg4MS+tnNmo7LrFFkqRzgY+65Ye6O+bVpZAu003d1",
                                                "buyer_stars": 12,
                                                "share_source": 0,
                                                "pv": 123,
                                                "status": 0,
                                                "tag_names": "女神",
                                                "comment": "非常好的评论"
                                            },
                                            {
                                                "picture": "http://img01.daily.taobaocdn.net/tfscom/i2/15782035348358/TB2BadXXXXXXXXEXXXXXXXXXXXX_ !!2066955782-0-rate.jpg",
                                                "pic_meta": "[{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i4/1862941194/TB2ER9mpVXXXXaCXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629},{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i2/1862941194/TB2PVygpVXXXXbBXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629},{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i2/1862941194/TB2t.9bpVXXXXbEXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629},{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i1/1862941194/TB2GyXQpVXXXXbvXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629}]",
                                                "item_id": 1500017235792,
                                                "id": 123,
                                                "description": "第二条发货快发货快发货快发货快发货快发货快发货快发货快发货快发货快",
                                                "seller_id": 2065340343,
                                                "buyer_nick": "2r***1",
                                                "mix_buyer_nick": "r01HKzPtEFlPg4MS+tnNmo7LrFFkqRzgY+65Ye6O+bVpZAu003d2",
                                                "buyer_stars": 12,
                                                "share_source": 0,
                                                "pv": 123,
                                                "status": 0,
                                                "tag_names": "女神",
                                                "comment": "非常好的评论"
                                            },
                                            {
                                                "picture": "http://img01.daily.taobaocdn.net/tfscom/i2/15782035348358/TB2BadXXXXXXXXEXXXXXXXXXXXX_ !!2066955782-0-rate.jpg",
                                                "pic_meta": "[{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i4/1862941194/TB2ER9mpVXXXXaCXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629},{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i2/1862941194/TB2PVygpVXXXXbBXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629},{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i2/1862941194/TB2t.9bpVXXXXbEXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629},{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i1/1862941194/TB2GyXQpVXXXXbvXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629}]",
                                                "item_id": 1500017235792,
                                                "id": 123,
                                                "description": "第三第发货快发货快发货快发货快发货快发货快发货快发货快发货快发货快",
                                                "seller_id": 2065340343,
                                                "buyer_nick": "3r***1",
                                                "mix_buyer_nick": "r01HKzPtEFlPg4MS+tnNmo7LrFFkqRzgY+65Ye6O+bVpZAu003d3",
                                                "buyer_stars": 12,
                                                "share_source": 0,
                                                "pv": 123,
                                                "status": 0,
                                                "tag_names": "女神",
                                                "comment": "非常好的评论"
                                            },
                                            {
                                                "picture": "http://img01.daily.taobaocdn.net/tfscom/i2/15782035348358/TB2BadXXXXXXXXEXXXXXXXXXXXX_ !!2066955782-0-rate.jpg",
                                                "pic_meta": "[{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i4/1862941194/TB2ER9mpVXXXXaCXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629},{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i2/1862941194/TB2PVygpVXXXXbBXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629},{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i2/1862941194/TB2t.9bpVXXXXbEXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629},{\"height\":411,\"pic\":\"https://img.alicdn.com/imgextra/i1/1862941194/TB2GyXQpVXXXXbvXXXXXXXXXXXX-1862941194.jpg\",\"size\":35522,\"width\":629}]",
                                                "item_id": 1500017235792,
                                                "id": 123,
                                                "description": "第四条发货快发货快发货快发货快发货快发货快发货快发货快发货快发货快",
                                                "seller_id": 2065340343,
                                                "buyer_nick": "4r***1",
                                                "mix_buyer_nick": "r01HKzPtEFlPg4MS+tnNmo7LrFFkqRzgY+65Ye6O+bVpZAu003d4",
                                                "buyer_stars": 12,
                                                "share_source": 0,
                                                "pv": 123,
                                                "status": 0,
                                                "tag_names": "女神",
                                                "comment": "非常好的评论"
                                            }
                                        ]
                                    },
                                    "page_size": 10,
                                    "page": 1
                                }
                            }
                        }');
            $res = array();
            $total = 0;
            if ($traderate->result->module->list->buyshow) {
                $total = $traderate->result->module->total_record;
                foreach ($traderate->result->module->list->buyshow as $item) {

                    $temp_item = array();
                    $temp_item['pic_meta'] = $item->pic_meta;
                    $temp_item['buyer_nick'] = $item->buyer_nick;
                    $temp_item['mix_buyer_nick'] = $item->mix_buyer_nick;
                    $temp_item['pic_meta'] = $item->pic_meta;
                    $temp_item['description'] = $item->description;


                    $item = json_decode(json_encode($item), true);
                    $avatar = $_mdl->getAvatar($item->mix_buyer_nick);
                    array_push($item, $avatar->user_avatar_get_response->avatar);
                    $temp_item['avatar'] = "https://img.alicdn.com/imgextra/i1/1862941194/TB2QrtOpVXXXXbOXpXXXXXXXXXX-1862941194.jpg";
                    array_push($res, $temp_item);
                }
            }

            $data = array(
                'list' => $res,
                'total' => $total
            );

            return $data;
        }
    }


    public function get_tags(){
        if (empty($this->tb_nick)) {
            return $this->ajax('', array(1, '未登录', 1, '请先登录应用.'));
        } else {
            if (isset($_REQUEST["itemId"])) {
                $itemId = $_REQUEST["itemId"];
                $data = $this->get_top_tags($itemId);
                return $this->ajax($data);
            }
        }
    }

    public function get_top_tags($itemId){
        if (empty($this->tb_nick)) {
            return $this->ajax('', array(1, '未登录', 1, '请先登录应用.'));
        } else {
            $_mdl = Loader::top('traderate_top');

            $tags = $_mdl->getTarderateTags($itemId);
            $tags = json_decode('{
                            "tags":{
                                "tmall_rate_tag_detail":[
                                    {
                                        "tag_name":"整体效果体效果体效果不错",
                                        "count":1,
                                        "posi":false
                                    },
                                    {
                                        "tag_name":"整体效果不错",
                                        "count":53,
                                        "posi":true
                                    },
                                    {
                                        "tag_name":"整体效",
                                        "count":50,
                                        "posi":true
                                    },
                                    {
                                        "tag_name":"整体效果不",
                                        "count":50,
                                        "posi":true
                                    }
                                ]
                            }
                        }');
            $ret = array();
            $total = 0;
            if ($tags->tags->tmall_rate_tag_detail) {
                foreach ($tags->tags->tmall_rate_tag_detail as $item) {
                    if ($item->posi) {
                        array_push($ret, $item);
                    }
                }
            }

            $data = array(
                'list' => $ret,
                'total' => $total
            );

            return $data;
        }
    }


    public function get_makeup(){
        if (empty($this->tb_nick)) {
            return $this->ajax('', array(1, '未登录', 1, '请先登录应用.'));
        } else {
            if (isset($_REQUEST["itemId"])) {
                $pageSize = isset($_REQUEST["pageSize"]) ? $_REQUEST["pageSize"] : 10;
                $pageNo = isset($_REQUEST["pageNo"]) ? $_REQUEST["pageNo"] : 1;
                $itemId = $_REQUEST["itemId"];

                $data = $this->get_top_makeup($itemId,$pageNo,$pageSize);
                return $this->ajax($data);
            }
        }
    }

    public function get_top_makeup($itemId,$pageNo,$pageSize){
        if (empty($this->tb_nick)) {
            return $this->ajax('', array(1, '未登录', 1, '请先登录应用.'));
        } else {
            $_mdl = Loader::top("makeup_top");

            $makeup = $_mdl->TmallSmartstoreMakeupMaterialGetRequest($itemId,$pageNo,$pageSize);
            $makeup = json_decode('{
                    "result": {
                        "data": {
                            "content_list": {
                                "dresser_query_result": [
                                    {
                                        "color": "212121",
                                        "custom_color": "000000",
                                        "item_id": "44018785809",
                                        "seller_id": 21451241,
                                        "seller_nick": "liangjiaoxj",
                                        "sku_id": "78733201605",
                                        "title": "口红1",
                                        "tmpl_dat": "http://algorithm-ps.oss-cn-hangzhou.aliyuncs.com/tmp/573f7c0be3e6f453bf48e9b0b68d00c1.dat",
                                        "tmpl_id": 14,
                                        "tmpl_img": "https://img.alicdn.com/tps/TB105dILpXXXXXrXXXXXXXXXXXX-120-66.png",
                                        "tmpl_title": "水润",
                                        "type": "KH",
                                        "unit_seq_id": "KH40001"
                                    },
                                    {
                                        "color": "000000",
                                        "custom_color": "333333",
                                        "item_id": "44018785809",
                                        "seller_id": 21451241,
                                        "seller_nick": "liangjiaoxj",
                                        "sku_id": "3104916398871",
                                        "title": "眉毛1",
                                        "tmpl_dat": "http://gw.alicdn.com/L1/429/49823646/zip/kds/meimao/1.zip",
                                        "tmpl_id": 23,
                                        "tmpl_img": "https://img.alicdn.com/tps/TB1AW4_LVXXXXb.XXXXXXXXXXXX-120-66.png",
                                        "tmpl_title": "1",
                                        "type": "MM",
                                        "unit_seq_id": "MM41001"
                                    },
                                    {
                                        "color": "c79c4e",
                                        "custom_color": "f30000",
                                        "item_id": "41217782468",
                                        "seller_id": 21451241,
                                        "seller_nick": "liangjiaoxj",
                                        "sku_id": "95585800286",
                                        "title": "眼线1",
                                        "tmpl_dat": "http://algorithm-ps.oss-cn-hangzhou.aliyuncs.com/tmp/7f3d906c094df2f3000229b81aa2e9db.dat",
                                        "tmpl_id": 7,
                                        "tmpl_img": "https://img.alicdn.com/tps/TB1DfXyLpXXXXayXpXXXXXXXXXX-120-66.png",
                                        "tmpl_title": "细",
                                        "type": "YX",
                                        "unit_seq_id": "YX42001"
                                    },
                                    {
                                        "color": "94649e",
                                        "custom_color": "96c000",
                                        "item_id": "41231348919",
                                        "seller_id": 21451241,
                                        "seller_nick": "liangjiaoxj",
                                        "sku_id": "3104429887393",
                                        "title": "眼影1",
                                        "tmpl_dat": "http://gw.alicdn.com/L1/429/49823646/zip/kds/yanying/60.zip",
                                        "tmpl_id": 22,
                                        "tmpl_img": "https://img.alicdn.com/tps/TB1wB8dLpXXXXXWaXXXXXXXXXXX-120-66.png",
                                        "tmpl_title": "小烟熏",
                                        "type": "YY",
                                        "unit_seq_id": "YY42002"
                                    },
                                    {
                                        "color": "080808",
                                        "custom_color": "600000",
                                        "item_id": "547284498269",
                                        "seller_id": 21451241,
                                        "seller_nick": "liangjiaoxj",
                                        "sku_id": "3312957868534",
                                        "title": "睫毛1",
                                        "tmpl_dat": "http://algorithm-ps.oss-cn-hangzhou.aliyuncs.com/tmp/d0eea71edeac1d6c32e043cabb817d3c.dat",
                                        "tmpl_id": 1,
                                        "tmpl_img": "https://img.alicdn.com/tps/TB1Rr0sLpXXXXaFXFXXXXXXXXXX-120-66.png",
                                        "tmpl_title": "自然",
                                        "type": "JM",
                                        "unit_seq_id": "JM43001"
                                    },
                                    {
                                        "color": "ff0000",
                                        "custom_color": "f00000",
                                        "item_id": "44018785809",
                                        "seller_id": 21451241,
                                        "seller_nick": "liangjiaoxj",
                                        "sku_id": "3104669734039",
                                        "title": "腮红1",
                                        "tmpl_dat": "http://algorithm-ps.oss-cn-hangzhou.aliyuncs.com/tmp/4be196e9e6e95f1ec5440d57937f86ea.dat",
                                        "tmpl_id": 16,
                                        "tmpl_img": "https://img.alicdn.com/tps/TB1QJxPLpXXXXc2XpXXXXXXXXXX-120-66.png",
                                        "tmpl_title": "2",
                                        "type": "SH",
                                        "unit_seq_id": "SH44001"
                                    }
                                ]
                            },
                            "count": 6
                        },
                        "succ": true,
                        "success": true
                    },
                    "request_id": "uakkjq229xz"
                }');
            $rs = array();
            $total = 0;
            $makeups_id = array();
            if ($makeup->result->data->content_list->dresser_query_result) {
                $total = $makeup->result->data->content_list->count;
                foreach ($makeup->result->data->content_list->dresser_query_result as $item) {
                    //$temp_tmpl = array();
                    array_push($makeups_id,$item->sku_id);
                    if($item->type == 'YX' || $item->type == 'SH' || $item->type == 'MM' || $item->type == 'YY'){
                        $_mdl = Loader::mdl('makeup_mdl');
                        $tmpls = $_mdl->get_tmpls();

                        foreach ($tmpls['list'] as $one){
                            /*array_push($temp_tmpl,array(
                                'tmpl_id' => $one->tmpl_id,
                                'tmpl_title' => $one->tmpl_title,
                                'tmpl_img' => $one->tmpl_img,
                                'tmpl_dat' => $one->tmpl_dat,
                            ));*/

                            $_mdl = Loader::mdl('makeup_mdl');
                            $rs = $_mdl->add_json(
                                $item->sku_id,
                                $item->title,
                                $item->type,
                                $item->color,
                                $item->custom_color,
                                $item->item_id,
                                $one['tmpl_id']
                                /*$one['tmpl_title'],
                                $one['tmpl_img']*/
                            );
                        }

                    }else{
                        /*array_push($temp_tmpl,array(
                            'tmpl_id' => $item->tmpl_id,
                            'tmpl_title' => $item->tmpl_title,
                            'tmpl_img' => $item->tmpl_img,
                            'tmpl_dat' => $item->tmpl_dat,
                        ));*/

                        $_mdl = Loader::mdl('makeup_mdl');
                        $rs = $_mdl->add_json(
                            $item->sku_id,
                            $item->title,
                            $item->type,
                            $item->color,
                            $item->custom_color,
                            $item->item_id,
                            $item->tmpl_id
                            /*$item->tmpl_title,
                            $item->tmpl_img*/
                        );
                    }
                }
            }

            //删除多余试妆数据
            $_mdl = Loader::mdl('makeup_mdl');
            $sql_makeups_id = $_mdl->get_makeups_id();
            $del_ids = array_diff($sql_makeups_id['list'],$makeups_id);
            $_mdl->del_makeups_ids($del_ids);

            $data = array(
                'list' => $rs,
                'total' => $total
            );

            return $data;
        }
    }


    public function get_makeup_tmpl(){
        if (empty($this->tb_nick)) {
            return $this->ajax('', array(1, '未登录', 1, '请先登录应用.'));
        } else {
            if (isset($_REQUEST["itemId"])) {

                $data = $this->get_top_makeup_tmpl();
                return $this->ajax($data);
            }
        }
    }

    public function get_top_makeup_tmpl(){
        if (empty($this->tb_nick)) {
            return $this->ajax('', array(1, '未登录', 1, '请先登录应用.'));
        } else {

            $rs = '';
            // 'JM','YX','KH','SH','MM','YY','ZT'
            $types = array('JM','YX','KH','SH','YY','ZT','MM');
            foreach($types as $type){
                $_mdl = Loader::top("makeup_top");
                $makeup_tmpl = $_mdl->TmallSmartstoreMakeupTemplateGetRequest($type);
                /*$makeup_tmpl = json_decode('{
                        "result":{
                            "succ":false,
                            "msg_code":"msgCode",
                            "datas":{
                                "dresser_template_info":[
                                    {
                                        "tmpl_dat":"http://gw.alicdn.com/L1/429/49823646/zip/kds/yanying/60.zip",
                                        "tmpl_img":"https://img.alicdn.com/tps/TB1Rr0sLpXXXXaFXFXXXXXXXXXX-120-66.png",
                                        "tmpl_title":"自然",
                                        "tmpl_id":14
                                    },
                                    {
                                        "tmpl_dat": "http://gw.alicdn.com/L1/429/49823646/zip/kds/meimao/1.zip",
                                        "tmpl_id": 23,
                                        "tmpl_img": "https://img.alicdn.com/tps/TB1AW4_LVXXXXb.XXXXXXXXXXXX-120-66.png",
                                        "tmpl_title": "1"
                                    },
                                    {
                                        "tmpl_dat": "http://algorithm-ps.oss-cn-hangzhou.aliyuncs.com/tmp/7f3d906c094df2f3000229b81aa2e9db.dat",
                                        "tmpl_id": 7,
                                        "tmpl_img": "https://img.alicdn.com/tps/TB1DfXyLpXXXXayXpXXXXXXXXXX-120-66.png",
                                        "tmpl_title": "细"
                                    },
                                    {
                                        "tmpl_dat": "http://gw.alicdn.com/L1/429/49823646/zip/kds/yanying/60.zip",
                                        "tmpl_id": 22,
                                        "tmpl_img": "https://img.alicdn.com/tps/TB1wB8dLpXXXXXWaXXXXXXXXXXX-120-66.png",
                                        "tmpl_title": "小烟熏"
                                    },
                                    {
                                        "tmpl_dat": "http://algorithm-ps.oss-cn-hangzhou.aliyuncs.com/tmp/d0eea71edeac1d6c32e043cabb817d3c.dat",
                                        "tmpl_id": 1,
                                        "tmpl_img": "https://img.alicdn.com/tps/TB1Rr0sLpXXXXaFXFXXXXXXXXXX-120-66.png",
                                        "tmpl_title": "自然"
                                    },
                                    {
                                        "tmpl_dat": "http://algorithm-ps.oss-cn-hangzhou.aliyuncs.com/tmp/4be196e9e6e95f1ec5440d57937f86ea.dat",
                                        "tmpl_id": 16,
                                        "tmpl_img": "https://img.alicdn.com/tps/TB1QJxPLpXXXXc2XpXXXXXXXXXX-120-66.png",
                                        "tmpl_title": "2"
                                    }
                                ]
                            },
                            "msg_info":"msgInfo",
                            "code":"IE001",
                            "success":true,
                            "msg":"CODE_SYS_ERR"
                        }
                    }');*/

                $rs = array();
                if ($makeup_tmpl->result->datas->dresser_template_info) {
                    foreach ($makeup_tmpl->result->datas->dresser_template_info as $item) {
                        $_mdl = Loader::mdl('makeup_mdl');
                        $rs = $_mdl->add_json_tmpl(
                            $item->tmpl_id,
                            $type,
                            $item->tmpl_title,
                            $item->tmpl_img
                        );
                    }
                }
            }


            $data = array(
                'list' => $rs
            );

            return $data;
        }
    }


    //扫描所有商品
    public function scan_all_item()
    {
        if (empty($this->tb_nick)) {
            return $this->ajax('', array(1, '未登录', 1, '请先登录应用.'));
        } else {
            $this->save_tm_cats();

            $keyword = '';
            $page_no = 1;
            $page_size = 200;
            $cids = 0;

            $_mdl = Loader::top('items_top');

            $key = $this->tb_nick . '_items';
            ccDel($key);
            $rs_items = ccLoad($key);
            //如果缓存中存在数据,直接从缓存中读取数据
            if ($rs_items != false) {
                $rs_items->getmode = 'cache';
                //var_dump($result->items->item);
                return $this->ajax($rs_items);
            } else {
                //取所有商品ID
                $top_items = $_mdl->ItemsOnsaleGetRequest($keyword, $page_no, $page_size, $cids);

                $items_id = array();
                foreach ($top_items->items->item as $item) {
                    array_push($items_id, $item->num_iid);
                }

                if ($top_items->total_results > 0) {
                    $pages = ceil($top_items->total_results / $page_size);
                    if ($pages > 0) {
                        for ($i = 0; $i >= $pages; $i++) {
                            $top_items = $_mdl->ItemsOnsaleGetRequest($keyword, $i + 1, $page_size, $cids);
                            foreach ($top_items->items->item as $item) {
                                array_push($items_id, $item->num_iid);
                            }
                        }
                    }
                }

                //试妆素材
                $this->get_top_makeup_tmpl();

                foreach ($items_id as $id) {
                    /*//$traderate = $_trd ->getTraderate($id);
                    $traderate = json_decode('{
                        "traderates_get_response":{
                            "trade_rates":{
                                "trade_rate":[{
                                        "nick":"张三",
                                        "result":"good",
                                        "created":"2010-01-01 13:30:05",
                                        "content":"很快劳而无功坩真坏时下时下是十是",
                                        "imgs":[
                                        {"url":"评价图"},{"url":"评价图"},{"url":"评价图"}
                                        ],
                                        "num_iid":1500006394531
                                    },
                                      {
                                        "nick":"张2",
                                        "result":"good",
                                        "created":"2010-01-01 13:30:05",
                                        "content":"很快劳而无功坩真坏时下时下是十是很快劳而无功坩真坏时下时下是十是",
                                        "imgs":[
                                        {"url":"评价图"},{"url":"评价图"},{"url":"评价图"}
                                        ],
                                        "num_iid":1500006394531
                                    },
                                      {
                                        "nick":"张4工三枯干枯干",
                                        "result":"good",
                                        "created":"2010-01-01 13:30:05",
                                        "content":"很快劳而无功坩真坏时下时下是十是很快劳而无功坩真坏时下时下是十是",
                                        "imgs":[
                                        {"url":"评价图"},{"url":"评价图"},{"url":"评价图"}
                                        ],
                                        "num_iid":1500006394531
                                    }
                                ]
                            },
                            "total_results":2,
                            "has_next":true
                        }
                    }');
                    $res = array();
                    if ($traderate->traderates_get_response->trade_rates) {
                        foreach ($traderate->traderates_get_response->trade_rates->trade_rate as $item) {
                            //取评论
                            $item = json_decode(json_encode($item), true);
                            //$avatar = $_trd->getAvatar($item->nick);
                            //array_push($item, $avatar->user_avatar_get_response->avatar);
                            $item['avatar'] = "头像";
                            array_push($res, $item);
                        }
                    }*/

                    $traderate = $this->get_top_traderate($id,1,3);

                    //取标签
                    $tags = $this->get_top_tags($id);

                    $_trdm = Loader::mdl('traderate_mdl');

                    $_trdm->add_json($id, addslashes(json_encode($traderate['list'])), addslashes(json_encode($tags['list'])));
                }

                //试妆数据
                //$this->get_top_makeup($id,1,400);

                $all_array = array_chunk($items_id, 20);
                $all_items = array();
                //根据ID取商品信息
                foreach ($all_array as $one_array) {
                    $one_ids = implode(',', $one_array);
                    $top_items = $_mdl->ItemsSellerListGetRequest($one_ids);

                    $all_items = array_merge($all_items, $top_items->items->item);
                }

                $top_items->items->item = $all_items;

                //查询商品折扣
                $top_items = $this->get_items_ump($top_items);

                $rs_items = (object)null;
                $rs_items->data = array();

                //商品主图
                function item_imgs($imgs)
                {
                    $rs_imgs = array();
                    for($i=0;$i<4;$i++){
                        if($imgs['item_img'][$i]){
                            array_push($rs_imgs, $imgs['item_img'][$i]['url']);
                        }
                    }

                    return implode(';', $rs_imgs);
                }

                foreach ($top_items->items->item as $item) {
                    $ump_price = $this->get_items_ump($item)->ump_price;

                    /*if($item->skus->sku){
                        $temp_sku = array();
                        foreach ($item->skus->sku as $sku_one){
                            $sku_one->sku_feature = json_decode('{
                              "color_hot_new": "new",
                              "color_material_img": "//img.alicdn.com/bao/uploaded/i3/3170729146/TB2Dv8WlypnpuFjSZFIXXXh2VXa_!!3170729146.jpg_140x140.jpg",
                              "color_value": "FF1493",
                              "color_material": "哑光",
                              "color_series": "正红色"
                            }');
                            array_push($temp_sku,$sku_one);
                        }
                        $item->skus->sku = $temp_sku;
                    }*/

                    $item = json_decode(json_encode($item), true);

                    $_mdl = Loader::mdl('items_mdl');
                    $rs = $_mdl->add_json(
                        $item['num_iid'],
                        preg_replace('/(.*?)(\d*?)/', '$2', $item['seller_cids']),
                        str_replace("+","%20",urlencode($item['title'])),
                        $item['price'],
                        $ump_price,
                        $item['period_sold_quantity'],
                        item_imgs($item['item_imgs']),
                        addslashes(json_encode($item['skus']['sku']))
                    );

                    array_push($rs_items->data, $item);
                }

                //删除多余商品
                $_mdl = Loader::mdl('items_mdl');
                $sql_items_id = $_mdl->get_items_id();
                $del_ids = array_diff($sql_items_id['list'],$items_id);
                $_mdl->del_items_ids($del_ids);

                //删除评价
                $_mdl = Loader::mdl('traderate_mdl');
                $_mdl->del_rates_ids($del_ids);


                ccSave($key, $rs_items, 3600);
                $rs_items->getmode = 'API';


                ccSave('scan_time', microtime(true), 36000);
            }

            return $this->ajax($rs_items);
        }
    }


    public function save_tm_cats()
    {
        if (empty($this->tb_nick)) {
            return $this->ajax('', array(1, '未登录', 1, '请先登录应用.'));
        } else {
            //$this->ajax(self::$seller);

            $key = $this->tb_nick . '_itemcats';
            ccDel($key);
            $all_cats = ccLoad($key);
            //如果缓存中存在数据,直接从缓存中读取数据
            if ($all_cats != false) {
                $all_cats->getmode = 'cache';
                //var_dump($result->items->item);
                return $this->ajax($all_cats);
            } else {
                $_mdl = Loader::top('items_top');
                $rs_cats = $_mdl->SellercatsListGetRequest($this->tb_nick);

                if (isset($rs_cats->msg)) {
                    $this->ajax('', $rs_cats);
                    return false;
                }
                //分类收集
                $cats_id = array();
                if($rs_cats->seller_cats){
                    $item_cats = o2a($rs_cats->seller_cats->seller_cat);
                    $all_cats = (object)null;
                    $all_cats->data = array();
                    foreach ($item_cats as $cat) {
                        $_mdl = Loader::mdl('cats_mdl');

                        array_push($cats_id,$cat['cid']);
                        array_push($all_cats->data, array(
                            'cid' => $cat['cid'],
                            'name' => $cat['name'],
                            'parent_cid' => $cat['parent_cid'],
                            'pic_url' => $cat['pic_url']
                        ));

                        $rs = $_mdl->add_cat(
                            $cat['cid'],
                            $cat['name'],
                            $cat['parent_cid'],
                            $cat['pic_url']
                        );

                    }

                    //删除多余分类
                    $_mdl = Loader::mdl('cats_mdl');
                    $sql_cats_id = $_mdl->get_cats_id();
                    $del_ids = array_diff($sql_cats_id['list'],$cats_id);
                    $_mdl->del_cats_ids($del_ids);

                    ccSave($key, $all_cats, 3600);
                    $all_cats->getmode = 'API';
                }
            }
            return true;
        }

    }

}
